package com.itheima.prize.api.action;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.prize.commons.config.RedisKeys;
import com.itheima.prize.commons.db.entity.CardGame;
import com.itheima.prize.commons.db.entity.CardUser;
import com.itheima.prize.commons.db.entity.CardUserDto;
import com.itheima.prize.commons.db.entity.ViewCardUserHit;
import com.itheima.prize.commons.db.mapper.ViewCardUserHitMapper;
import com.itheima.prize.commons.db.service.GameLoadService;
import com.itheima.prize.commons.db.service.ViewCardUserHitService;
import com.itheima.prize.commons.utils.ApiResult;
import com.itheima.prize.commons.utils.PageBean;
import com.itheima.prize.commons.utils.RedisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Set;

@RestController
@RequestMapping(value = "/api/user")
@Api(tags = {"用户模块"})
public class UserController {

    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private ViewCardUserHitService hitService;
    @Autowired
    private GameLoadService loadService;

    /**
     * 获取用户信息
     *
     * @param request
     * @return
     */
    @GetMapping("/info")
    @ApiOperation(value = "用户信息")
    public ApiResult info(HttpServletRequest request) {
        //获取session中的用户信息
        Object user = request.getSession().getAttribute("user");
        //判断session是否有用户信息
        if (user == null) {
            return new ApiResult<>(0, "登入超时", null);
        }
        CardUserDto cardUserDto = new CardUserDto();

        //拷贝user属性到CardUserDto
        BeanUtils.copyProperties(user, cardUserDto);

        //查询活动次数和中奖次数，并设置属性
        Integer userId = cardUserDto.getId();
        cardUserDto.setGames(loadService.getGamesNumByUserId(userId));
        cardUserDto.setProducts(loadService.getPrizesNumByUserId(userId));
        return new ApiResult<>(1, "成功", cardUserDto);
    }

    /**
     * 查询我的奖品
     *
     * @param gameid
     * @param curpage
     * @param limit
     * @param request
     * @return
     */
    @GetMapping("/hit/{gameid}/{curpage}/{limit}")
    @ApiOperation(value = "我的奖品")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "gameid", value = "活动id（-1=全部）", dataType = "int", example = "1", required = true),
            @ApiImplicitParam(name = "curpage", value = "第几页", defaultValue = "1", dataType = "int", example = "1"),
            @ApiImplicitParam(name = "limit", value = "每页条数", defaultValue = "10", dataType = "int", example = "3")
    })
    public ApiResult hit(@PathVariable int gameid, @PathVariable int curpage, @PathVariable int limit, HttpServletRequest request) {
        //获取用户id
        CardUser user = (CardUser) request.getSession().getAttribute("user");
        Integer userId = user.getId();

        //设置页码和页大小
        Page<ViewCardUserHit> page = new Page<>(curpage, limit);

        //分页查询我的奖品
        Page<ViewCardUserHit> p;
        if (gameid == -1) {
            p = hitService.lambdaQuery()
                    .eq(ViewCardUserHit::getUserid, userId)
                    .page(page);
        } else {
            p = hitService.lambdaQuery()
                    .eq(ViewCardUserHit::getUserid, userId)
                    .eq(ViewCardUserHit::getGameid, gameid)
                    .page(page);
        }

        //类型转换
        PageBean<ViewCardUserHit> pageBean = new PageBean<>(p);

        return new ApiResult<>(1, "成功", pageBean);
    }


}